import json
import re
from collections import Counter
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from textblob import TextBlob
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk

# 下载NLTK数据
nltk.download('stopwords')
nltk.download('punkt_tab')

# 1. 加载JSON文件中的评论
def load_comments(json_file):
    with open(json_file, 'r', encoding='utf-8') as f:
        return json.load(f)

# 2. 英文文本清洗
def clean_english_text(text):
    # 转小写 + 移除标点
    text = text.lower()
    text = re.sub(r'[^\w\s]', '', text)
    # 分词 + 移除停用词
    words = word_tokenize(text)
    stop_words = set(stopwords.words('english'))
    words = [word for word in words if word not in stop_words and len(word) > 2]
    return ' '.join(words)

# 3. 词频统计
def get_word_frequency(comments, top_n=20):
    all_words = []
    for comment in comments:
        cleaned = clean_english_text(comment['text'])
        all_words.extend(cleaned.split())
    return Counter(all_words).most_common(top_n)

# 4. 情感分析（使用TextBlob）
def analyze_sentiment(text):
    analysis = TextBlob(text)
    return analysis.sentiment.polarity  # 范围: [-1, 1]

def get_sentiment_stats(comments):
    sentiments = [analyze_sentiment(c['text']) for c in comments]
    return {
        'average': sum(sentiments) / len(sentiments),
        'positive': sum(1 for s in sentiments if s > 0.1),
        'negative': sum(1 for s in sentiments if s < -0.1),
        'neutral': sum(1 for s in sentiments if -0.1 <= s <= 0.1)
    }

# 5. 可视化
def plot_wordcloud(comments):
    all_text = ' '.join([clean_english_text(c['text']) for c in comments])
    wordcloud = WordCloud(width=800, height=400, background_color='white').generate(all_text)
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis('off')
    plt.show()

def plot_sentiment(stats):
    labels = ['Positive', 'Neutral', 'Negative']
    sizes = [stats['positive'], stats['neutral'], stats['negative']]
    colors = ['#66b3ff', '#99ff99', '#ff9999']
    plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
    plt.title('Comment Sentiment Distribution')
    plt.show()

# 主程序
def main():
    # 加载评论
    comments = load_comments('comments_l7S3fTAZUgk.json')
    
    # 词频分析
    word_freq = get_word_frequency(comments)
    print("Top 20 Words:", word_freq)
    
    # 情感分析
    sentiment_stats = get_sentiment_stats(comments)
    print(f"Average Sentiment: {sentiment_stats['average']:.2f}")
    print(f"Positive Comments: {sentiment_stats['positive']}")
    print(f"Negative Comments: {sentiment_stats['negative']}")
    print(f"Neutral Comments: {sentiment_stats['neutral']}")
    
    # 可视化
    plot_wordcloud(comments)
    plot_sentiment(sentiment_stats)

if __name__ == '__main__':
    main()